web/src/pages/[category].astro

102 lines · 2379 bytes

  1---
  2
  3import Layout from '@layouts/Layout.astro';
  4import SectionList from '@components/things/SectionList.astro';
  5import Main from '@components/scafold/MainCard.astro';
  6
  7import { fetchData, slugify } from '@utils/fetch-data';
  8
  9import type { Section } from '../types/Service';
 10import Button from '@components/form/Button.astro';
 11
 12interface Props {
 13	title: string;
 14	sections: Section[];
 15}
 16
 17const { title, sections } = Astro.props;
 18
 19const categories = (await fetchData()).categories;
 20
 21export async function getStaticPaths() {
 22	const pages = await fetchData().then((data) => {
 23		return data.categories.map((category) => {
 24			return {
 25				category: slugify(category.name),
 26				title: category.name,
 27				sections: category.sections,
 28			};
 29		});
 30	});
 31
 32  return pages.map(({ category, title, sections }) => {
 33    return {
 34      params: { category },
 35      props: { title, sections },
 36    };
 37  });
 38}
 39
 40const makePaginationLinks = () => {
 41  const index = categories.findIndex(category => category.name === title);
 42  const previousCategory = index >  0 ? categories[index -  1].name : null;
 43  const nextCategory = index < categories.length -  1 ? categories[index +  1].name : null;
 44  return { previous: previousCategory, next: nextCategory };
 45};
 46
 47const { previous, next } = makePaginationLinks();
 48
 49---
 50
 51<Layout title={`${title} | Awesome Privacy`}>
 52	<Main>
 53		<SectionList title={title} sections={sections} bigTitle={true} />
 54		<div class="pagination-navigation">
 55      { previous ? (
 56        <Button url={`/${slugify(previous)}`}>
 57          <span>← Previous</span>
 58          <p>{previous}</p>
 59        </Button>
 60      ) : <p class="nothing"></p>}
 61      { next && (
 62        <Button url={`/${slugify(next)}`}>
 63          <span>Next →</span>
 64          <p>{next}</p>
 65        </Button>
 66      )}
 67    </div>
 68	</Main>
 69</Layout>
 70
 71<style lang="scss">
 72.pagination-navigation {
 73  display: flex;
 74  justify-content: space-between;
 75  align-items: center;
 76  padding: 1rem 0;
 77  :global(.button) {
 78    min-width: 120px;
 79    width: fit-content;
 80    padding: 0.25rem 1rem;
 81    text-align: right;
 82    &:first-child { text-align: left; }
 83    p {
 84      margin: 0;
 85      font-weight: normal;
 86      font-size: 1rem;
 87    }
 88    span {
 89      font-size: 0.8rem;
 90    }
 91    
 92  }
 93  .nothing {
 94    width: 120px;
 95  }
 96  .go-to-category {
 97    color: var(--foreground);
 98    font-size: 0.8rem;
 99    opacity: 0.5;
100  }
101}
102</style>